import 'package:flutter/material.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/config/app_config.dart';
import 'package:flutter_base/generated/assets.dart';
import 'package:flutter_base/widgets/image/auto_image.dart';
import 'package:flutter_base/widgets/image/image_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TestBadgeBlock extends StatelessWidget {
  final Map? data;

  const TestBadgeBlock({super.key, this.data});

  @override
  Widget build(BuildContext context) {
    List badges = [];
    if (data == null || data?.keys.contains('badges') == false) {
      badges = [
        {
          "description": "Creative Thinking",
          "icon": Assets.testBadge0,
          "name": "You excel in Innovative Thinking"
        },
        {
          "description": "Adaptability",
          "icon": Assets.testBadge1,
          "name": "You excel in Adaptability"
        },
        {
          "description": "Leadership",
          "icon": Assets.testBadge2,
          "name": "You excel in Leadership"
        },
        {
          "description": "Team Builder",
          "icon": Assets.testBadge3,
          "name": "You excel in Team Builder"
        },
        {
          "description": "Master Communicator",
          "icon": Assets.testBadge4,
          "name": "You excel in Master Communicator"
        },
        {
          "description": "Student Learner",
          "icon": Assets.testBadge5,
          "name": "You excel in Student Learner"
        },
      ];
    } else {
      badges = data?['badges'] ?? [];
    }

    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(24.w),
      ),
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 24.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 标题和说明文字
          Text(
            'Your trait badges',
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w600,
              color: AppColor.color0C0A09,
              letterSpacing: 0.14,
              height: 1.25,
            ),
          ),
          SizedBox(height: 4.w),
          Text(
            'Congratulations on earning the following achievement badges',
            style: TextStyle(
              fontSize: 12.sp,
              color: const Color(0xFF7F909F),
              height: 1.25,
            ),
          ),
          SizedBox(height: 12.w),

          ...badges.map((item) {
            return _buildBadgeItem(
                imageUrl: item['icon'],
                title: item['name'],
                subtitle: item['description']);
          })
        ],
      ),
    );
  }

  Widget _buildBadgeItem(
      {required String imageUrl,
      required String title,
      required String subtitle,
      bool isImageUrl = true}) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 12.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.w),
        boxShadow: [
          BoxShadow(
            color: const Color(0x1A646464),
            offset: Offset(0, 8.w),
            blurRadius: 33.w,
          ),
        ],
      ),
      padding: EdgeInsets.all(12.w),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 徽章图标
          isImageUrl
              ? Container(
                  width: 48.w,
                  height: 48.w,
                  decoration: BoxDecoration(
                    color: const Color(0xFFFAFAF9),
                    borderRadius: BorderRadius.circular(12.w),
                  ),
                  alignment: Alignment.center,
                  child: ImageView(
                    imageUrl: imageUrl,
                    width: 32.w,
                    height: 32.w,
                    fit: BoxFit.cover,
                    radius: 0,
                  ),
                )
              : AutoImage(
                  imageUrl,
                  width: 48.w,
                ),
          SizedBox(width: 8.w),
          // 文字内容
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w600,
                    color: AppColor.color0C0A09,
                    letterSpacing: 0.14,
                    height: 1.25,
                  ),
                ),
                SizedBox(height: 4.w),
                Text(
                  subtitle,
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: const Color(0xFF7F909F),
                    height: 1.25,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
